var xmlhttp;
var setId = -1;
var tempType = 0;

function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function updateListening()
{
	updateMachine(1);
}
function updateSpeaking()
{
	updateMachine(2);
}
function updateReading()
{
	updateMachine(3);
}
function updateWriting()
{
	updateMachine(4);
}

function updateMachine(type)
{
	getXmlHttpObject();
	var content;
	var topicId;
	var title;
	tempType = type;
	if(type == 1){
		content = document.getElementById("listeningContent").value;
		topicId = document.getElementById("listeningId").value;
		title = document.getElementById("listeningSummary").value;
		
	}
	else if(type == 2){
		content = document.getElementById("speakingContent").value;
		topicId = document.getElementById("speakingId").value;
		title = document.getElementById("speakingSummary").value;
	}
	else if(type == 3){
		content = document.getElementById("readingContent").value;
		topicId = document.getElementById("readingId").value;
		title = document.getElementById("readingSummary").value;
	}
	else if(type == 4){
		content = document.getElementById("writingContent").value;
		topicId = document.getElementById("writingId").value;
		title = document.getElementById("writingSummary").value;
	}
	if(topicId == "")
	{
		addMachine(type, content, title);
	}
	else
	{
		var url = "../controllers/updateTopic.php";
		params = "content=" + encodeURIComponent(content) + "&topicId=" + topicId + "&title=" + title;
		
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange = updateMachineSuccess;
		xmlhttp.send(params);
	}
}

function updateMachineSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var buttonDiv;
			if(tempType == 1){
				buttonDiv = document.getElementById("listeningDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>修改听力完成</p>";
			}
			else if(tempType == 2){
				buttonDiv = document.getElementById("speakingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>修改口语完成</p>";
			}
			else if(tempType == 3){
				buttonDiv = document.getElementById("readingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>修改阅读完成</p>";
			}
			else if(tempType == 4){
				buttonDiv = document.getElementById("writingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>修改写作完成</p>";
			}
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function addMachine(type, content, title)
{
	var url = "../controllers/addTopic.php";
	params = "content=" + encodeURIComponent(content) + "&type=" + type + "&setid=" + setId + "&title=" + title;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = updateMachineSuccess;
	xmlhttp.send(params);
}


function updateSetInfo(setId)
{
	getXmlHttpObject();

	var title = document.getElementById("title").value;
	var url = "../controllers/updateSet.php";
	params = "title=" + title + "&setId=" + setId;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = updateSetInfoSuccess;
	xmlhttp.send(params);
}

function updateSetInfoSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			alert("修改机经名称成功！");
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteTopic(type, topicId, setId){
	getXmlHttpObject();
	tempType = type;
	var url = "../controllers/deleteTopic.php";
	params = "topicId=" + topicId + "&setId=" + setId + "&type=" + type;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = deleteTopicSuccess;
	xmlhttp.send(params);
}

function deleteTopicSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			location.reload();
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}
